package com.tc.tickets.push;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.tc.tickets.push.JPushUtil;
import com.tc.tickets.push.Push;
import com.tc.tickets.push.PushDebugUtil;

import cn.jpush.android.api.JPushInterface;


/**
 * YaoWen(43194) create at tongcheng work pc,
 * time:  2017/3/1 15:20  qq:2669932513
 */
public class MyReceiver extends BroadcastReceiver{

    public static final String TAG = "MyReceiver";

    private Context mContext;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;

        String action = intent.getAction();
        PushDebugUtil.i(TAG, "action = " + action);

        if (action.equals(JPushInterface.ACTION_MESSAGE_RECEIVED)){
            handCustomMessage(intent);
        }

        if (action.equals(JPushInterface.ACTION_NOTIFICATION_OPENED)){
            PushDebugUtil.i(TAG, "用户打开了 notification");
        }
    }

    private void handCustomMessage(Intent intent){
        Bundle bundle = intent.getExtras();


        StringBuilder info = new StringBuilder();
        info.append("title=" + bundle.getString(JPushInterface.EXTRA_TITLE));
        info.append("\t msg=" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
        info.append("\t extras=" + bundle.getString(JPushInterface.EXTRA_EXTRA));
        info.append("\t msgId=" + bundle.getString(JPushInterface.EXTRA_MSG_ID));

        PushDebugUtil.i(TAG, info.toString());
        PushDebugUtil.showToast(mContext, info.toString());
    }
}
